package controller

import (
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"golang.org/x/crypto/bcrypt"
	"hytx_manager/form"
	"hytx_manager/models"
	"hytx_manager/pkg/setting"
	"strconv"
	"time"
)

func ManageCenterList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	data, paginate := models.ManageCenterList(page, limit, c)
	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}
func ManageCenterAdd(c *gin.Context) {

	var m form.ManageCenterForm
	c.ShouldBind(&m)
	valid := validation.Validation{}
	b, err := valid.Valid(&m)
	if err != nil {
		fail(c, "")
		return
	}
	if !b {
		for _, err := range valid.Errors {
			fail(c, err.Key+" "+err.Message)
			return
		}
	}
	var us models.Users
	models.DB.Table("users").Where("account=?", m.Phone).Or("phone=?",m.Phone).First(&us)
	var m1 models.ManageCenter
	models.DB.Model(models.ManageCenter{}).Where("admin_mobile_phone=?", m.Phone).First(&m1)
	var o1 models.Operators
	models.DB.Model(models.Operators{}).Where("phone=?", m.Phone).First(&o1)
	if us.Account == m.Phone || us.Phone == m.Phone || m1.AdminMobilePhone == m.Phone || o1.Phone == m.Phone{
		fail(c, "该手机号已被使用")
		return
	}
	if !models.NewSMSValidate(m.Phone).ValidateSMS(m.Code) {
		fail(c, "验证码错误或超时")
		return
	}
	//r := util.Krand(6, util.KC_RAND_KIND_ALL)
	pass, err := bcrypt.GenerateFromPassword([]byte("123456"), 10)
	mc := &models.ManageCenter{
		Title:            m.Title,
		BusinessLicense:  m.BusinessLicense,
		AdminName:        m.Username,
		IdCard:           m.IDCard,
		IdCardFront:      m.IDCardFront,
		IdCardBack:       m.IDCardBack,
		Province:         m.Province,
		City:             m.City,
		Area:             m.Area,
		AdminMobilePhone: m.Phone,
		BankCardNo:       m.BankCardNo,
		BankCardName:     m.BankCardName,
		BankName:         m.BankName,
		OperatorsNum:m.OperatorsNum,
		CreatedAt:        time.Now(),
	}

	u := &models.Users{
		Account:  m.Phone,
		Phone:    m.Phone,
		Password: string(pass),
		Type:     1,
	}
	if !u.Insert() {
		serverError(c)
		return
	}
	mc.UserId = u.ID
	mc.Insert()
	//发送短信
	success(c)
}

func ManageCenterEdit(c *gin.Context) {
	var m form.ManageCenterForm
	c.ShouldBind(&m)
	id := c.PostForm("id")

	mc := &models.ManageCenter{
		Title:            m.Title,
		BusinessLicense:  m.BusinessLicense,
		AdminName:        m.Username,
		IdCard:           m.IDCard,
		IdCardFront:      m.IDCardFront,
		IdCardBack:       m.IDCardBack,
		Province:         m.Province,
		City:             m.City,
		Area:             m.Area,
		AdminMobilePhone: m.Phone,
		BankCardNo:       m.BankCardNo,
		BankCardName:     m.BankCardName,
		BankName:         m.BankName,
		OperatorsNum:     m.OperatorsNum,
		UpdatedAt:        time.Now(),
	}
	var mo models.ManageCenter
	models.DB.Model(models.ManageCenter{}).Where("id=?", id).First(&mo)
	if m.Phone != "" && m.Phone != mo.AdminMobilePhone {
		var us models.Users
		models.DB.Table("users").Where("account=?", m.Phone).Or("phone=?",m.Phone).First(&us)
		var m1 models.ManageCenter
		models.DB.Model(models.ManageCenter{}).Where("admin_mobile_phone=?", m.Phone).First(&m1)
		var o1 models.Operators
		models.DB.Model(models.Operators{}).Where("phone=?", m.Phone).First(&o1)
		if us.Account == m.Phone || us.Phone == m.Phone || m1.AdminMobilePhone == m.Phone || o1.Phone == m.Phone{
			fail(c, "该手机号已被使用")
			return
		}
		if !models.NewSMSValidate(m.Phone).ValidateSMS(m.Code) {
			fail(c, "验证码错误或超时")
			return
		}
		//r := util.Krand(6, util.KC_RAND_KIND_ALL)
		pass, _ := bcrypt.GenerateFromPassword([]byte("123456"), 10)
		var u models.Users

		models.DB.Table("users").Where("id=?", mo.UserId).First(&u)
		u.Password = string(pass)
		u.Account = m.Phone
		u.Phone = m.Phone
		u.Update()
		//发送短信
	}

	mc.Update(id)
	success(c)
}

func ManageCenterGet(c *gin.Context) {
	id := c.Query("id")
	var m models.ManageCenter
	models.DB.Model(models.ManageCenter{}).Where("id=?", id).First(&m)
	render(c, m)
}

func ManageCenterDel(c *gin.Context) {
	id := c.Query("id")
	var m models.ManageCenter
	models.DB.Model(models.ManageCenter{}).Where("id=?", id).First(&m)
	models.DB.Delete(&models.ManageCenter{}, "id=?", id)
	models.DB.Delete(&models.Users{} ,"id=?", m.UserId)
	success(c)
}
func ManageCenterAble(c *gin.Context) {
	id,_ := strconv.Atoi(c.PostForm("id"))
	status,_ := strconv.Atoi(c.PostForm("state"))
	models.DB.Model(&models.ManageCenter{}).Where("id=?", id).Update("status", status)
	u := models.GetManageCenterById(id)
	models.DB.Table("users").Where("id=?", u.UserId).Update("is_enabled", status)
	success(c)
}
